package dao;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import server.User;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class Users implements UserMapper {
    SqlSession session=null;
    public Users() throws IOException {
        SqlSessionFactoryBuilder ssfb=new SqlSessionFactoryBuilder();
        InputStream is= Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactory ssf=ssfb.build(is);
        session=ssf.openSession();
    }
    public int insertUser(User record) {
        int i=session.insert("dao.Users.insertUser",record);
        session.commit();
        System.out.println(i);
        return i;
    }
    public int Oneselectphone(String value){
        Map<String,String> map=new HashMap<String, String>();
        map.put("value",value);
        int count=session.selectOne("dao.Users.Oneselectphone",map);
        return count;
    }
    public int Oneselectusername(String value){
        Map<String,String> map=new HashMap<String, String>();
        map.put("value",value);
        int count=session.selectOne("dao.Users.Oneselectusername",map);
        return count;
    }
    public String  OneselectUserPass(String user,String pass){
        Map<String,String> map=new HashMap<String, String>();
        map.put("user",user);
        map.put("pass",pass);
        Map maps=session.selectOne("dao.Users.OneselectUserPass",map);
        System.out.println(maps);
        if(maps!=null){
            return (String) maps.get("username");
        }else{
            return null;
        }
    }

//    public static void main(String[] args) {
//        try {
//            new Users().OneselectUserPass("aaa","bbb");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
}
